# 功能：后端服务框架，负责前后端交互
# 导入Flask类
from flask import Flask, jsonify
from flask import request
from flask_cors import CORS
from scan_tool import directory_match
from sub_functions import weight_calculation
import json
# 实例化，可视为固定格式
app = Flask(__name__)

@app.route('/callAlgorithm', methods=['POST'])
def callAlgorithm():
    if request.method == 'POST':
        headers = request.headers
        data = request.get_json()
        if data['algorithmName'] == 'scan_tool':

            para = json.loads(data['algorithmPara'])
            keywords = para['keywords'].split(';')

            ret = directory_match(project_dir=para['directory'],
                               keywords=keywords,
                               prefix=para['prefix'],
                               postfix=para['postfix'])
            return json.dumps(ret)
        if data['algorithmName'] == 'weight_calculation':
            ret = weight_calculation(data['rootPath'],
                               data['algorithmName'],
                               data['algorithmType'],
                               data['algorithmPara'],
                               data['additionalPara'])
            return json.dumps(ret)

if __name__ == '__main__':
    # app.run(host, port, debug, options)
    # 默认值：host=127.0.0.1, port=5000, debug=false

    CORS(app)
    app.run()   #启动后端